package com.woniuxy.controller;

import com.woniuxy.entity.ArticleRecord;
import com.woniuxy.entity.DTO.ArticleRecordQueryDTO;
import com.woniuxy.service.IArticleRecordService;
import com.woniuxy.util.PageResult;
import com.woniuxy.util.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author pml
 * @Date 2025/7/21 10:28
 * @Description TODO
 * 帖子审核记录控制器
 */
@RestController
@RequestMapping("/articleRecord")
public class ArticleRecordController {

    @Autowired
    private IArticleRecordService articleRecordService;

    /**
     * 多条件分页查询
     */
    @GetMapping("/list")
    public ResponseData<PageResult<ArticleRecord>> pageQuery(ArticleRecordQueryDTO queryDTO) {
        PageResult<ArticleRecord> pageResult = articleRecordService.pageQuery(queryDTO);
        return ResponseData.ok(pageResult);
    }

    /**
     * 新增审核记录
     */
    @PostMapping
    public ResponseData add(@RequestBody ArticleRecord articleRecord) {
        boolean save = articleRecordService.save(articleRecord);
        return save ? ResponseData.ok() : ResponseData.fail(500, "新增失败");
    }

    /**
     * 更新审核记录
     */
    @PutMapping
    public ResponseData update(@RequestBody ArticleRecord articleRecord) {
        boolean update = articleRecordService.updateById(articleRecord);
        return update ? ResponseData.ok() : ResponseData.fail(500, "更新失败");
    }

    /**
     * 单个删除
     */
    @DeleteMapping("/{id}")
    public ResponseData delete(@PathVariable String id) {
        boolean remove = articleRecordService.removeById(id);
        return remove ? ResponseData.ok() : ResponseData.fail(500, "删除失败");
    }

    /**
     * 批量删除
     */
    @DeleteMapping("/batch")
    public ResponseData batchDelete(@RequestBody List<String> ids) {
        boolean remove = articleRecordService.removeByIds(ids);
        return remove ? ResponseData.ok() : ResponseData.fail(500, "批量删除失败");
    }

    /**
     * 批量更新审核状态（已读/未读）
     */
    @PutMapping("/read/batch")
    public ResponseData batchUpdateRead(
            @RequestParam Integer auditResult,
            @RequestParam(required = false) String auditOpinion,
            @RequestBody List<String> ids) {
        boolean success = articleRecordService.batchUpdateAuditResult(ids, auditResult, auditOpinion);
        return success ? ResponseData.ok() : ResponseData.fail(500, "批量更新失败");
    }
}